package com.hainiu.cat.web.consumer;

import com.hainiu.cat.web.config.TopicRabbitConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * create by biji.zhao on 2021/6/4
 */
@Component
@Slf4j
public class TopicListenerTwo implements ChannelAwareMessageListener {
    @Override
//    @RabbitListener(queues = TopicRabbitConfig.TOPIC_SECOND_QUEUE)
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            String msg = message.toString();
            log.info("topic模式 监听者 two  接收到数据:{}",msg);
            //为true表示确认之前的所有消息  false表示只来处理着当前的消息
            channel.basicAck(deliveryTag, false);
        }catch (Exception e) {
            log.info("topic模式 监听者 one  处理消息时显示异常,异常是:{},现拒绝消费当前消息且不再放回队列",e.getMessage());
            //为true会重新放回队列
            channel.basicReject(deliveryTag, false);
        }
    }
}
